package com.sysu.ooad.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.Action;
import com.sysu.ooad.dto.TaskFacade;
import com.sysu.ooad.entity.Group;
import com.sysu.ooad.entity.Homework;
import com.sysu.ooad.entity.Student;
import com.sysu.ooad.entity.Task;
import com.sysu.ooad.service.TaskService;
import com.sysu.ooad.util.AjaxCommand;

@Component
@Scope("prototype")
public class HomeworkStudentAction extends CommonAction {

	private List<Homework> myHomeworks;
	private List<Task> notCloseTasks;
	private TaskService taskService;
	private Task task;
	private Group group;
	private List<Task> taskList;
	
	public List<Task> getTaskList() {
		return taskList;
	}
	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}
	public Task getTask() {
		return task;
	}
	public void setTask(Task task) {
		this.task = task;
	}
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	public List<Task> getNotCloseTasks() {
		return notCloseTasks;
	}
	public void setNotCloseTasks(List<Task> notCloseTasks) {
		this.notCloseTasks = notCloseTasks;
	}
	public List<Homework> getMyHomeworks() {
		return myHomeworks;
	}
	public void setMyHomeworks(List<Homework> myHomeworks) {
		this.myHomeworks = myHomeworks;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	@Resource
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	@Override
	public String execute() throws Exception {
		Student s = this.findCurrentStudent();
		myHomeworks = taskService.findHomeworksByStudent(s);
		notCloseTasks = taskService.findNotCloseTasksByCourse(s.getCourse());
		taskList = taskService.findTasksByCourse(s.getCourse());
		return Action.SUCCESS;
	}
	
	public String showHomeworksByGroupAndTask() throws Exception {
		Student s = this.findCurrentStudent();
		task = taskService.findTaskById(task.getId());
		TaskFacade taskFacade = new TaskFacade(task);
		List<Homework> homeworks = taskService.findHomeworksByTaskAndGroup(task, s.getGroup());
		for(Homework h : homeworks) {
			taskFacade.addHomework(h);
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		AjaxCommand.ajaxReturnJSON(response, taskFacade);
		return null;
	}
}
